package mylogger

import (
	"fmt"
	"time"
)

// 往终端写日志
type ConsoleLogger struct {
	Level LogLevel
}

func NewLog(levelStr string) ConsoleLogger {
	return ConsoleLogger{
		Level: parseLogLevel(levelStr),
	}
}

func (c ConsoleLogger) compare(level LogLevel) bool {
	return level >= c.Level
}

func (c ConsoleLogger) log(level LogLevel, format string, a ...interface{}) {
	if c.compare(level) {
		msg := fmt.Sprintf(format, a...)
		now := time.Now()
		funcName, fileName, lineNum := getInfo(3)
		fmt.Printf("[%s] [%s] [%s:%s:%d] %s\n", now.Format("2006-01-02,15:04:05"), levelToStr(level), fileName, funcName, lineNum, msg)
	}
}

func (c ConsoleLogger) Debug(msg string, a ...interface{}) {
	c.log(DEBUG, msg, a...)
}

func (c ConsoleLogger) Info(msg string, a ...interface{}) {
	c.log(INFO, msg, a...)
}

func (c ConsoleLogger) Warning(msg string, a ...interface{}) {
	c.log(WARNING, msg, a...)
}

func (c ConsoleLogger) Error(msg string, a ...interface{}) {
	c.log(ERROR, msg, a...)
}

func (c ConsoleLogger) Fatal(msg string, a ...interface{}) {
	c.log(FATAL, msg, a...)
}
